﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	FormsRulesSource.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.Linq;
using Smart.Common.Rules;

namespace Smart.Common.Forms
{
	/// <summary>
	/// Forms rules source
	/// </summary>
	public class FormsRulesSource : XmlRulesSource
	{
		/// <summary>
		/// Forms rules source
		/// </summary>
		public FormsRulesSource(RulesCtx ctx)
			: base(ctx)
		{
		}

		/// <summary>
		/// Rules filter
		/// </summary>
		public override RulesFilter CreateFilter()
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Load instance by instance id
		/// </summary>
		/// <param name="instanceId">Instance id</param>
		/// <returns>Instance of TInstanceType</returns>
		public override IRulesInstance LoadInstance(string instanceId)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Load rules instance by rules filter
		/// </summary>
		/// <param name="filter">Filter to load rules instance</param>
		/// <returns>Rules instance</returns>
		public override IRulesInstance LoadInstance(RulesFilter filter)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Parse rules and load to context
		/// </summary>
		public override void ParseRulesToCtx(object rulesSource)
		{
			// Parse choose rules?
			if (rulesSource is MetaFormsRules)
			{
				var rules = (MetaFormsRules)rulesSource;

				Ctx.Rules.AddRange(rules.Select(rule => rule.Rule));
			}
			// Parse action rules?
			else if (rulesSource is MetaFormRules)
			{
				var rules = (MetaFormRules)rulesSource;

				Ctx.Rules.AddRange(rules.Select(rule => rule.Rule));
			}
			else
			{
				throw new NotSupportedException();
			}
		}

		/// <summary>
		/// Parse rule
		/// </summary>
		public override IRule ParseRule(object ruleSource)
		{
			// Parse choose rule?
			var formsRule = ruleSource as MetaFormsRule;

			if (formsRule != null)
				return formsRule.Rule;

			// Parse action rule?
			var formRule = ruleSource as MetaFormRule;

			if (formRule != null)
				return formRule.Rule;

			throw new NotSupportedException();
		}

		/// <summary>
		/// Load instance variables to context
		/// </summary>
		public override void LoadInstanceVariablesToCtx()
		{
			Dictionary<string, object> dv = null;

			if (Ctx.Instance is FormsChooseRulesInstance)
			{
				var ri = (FormsChooseRulesInstance)Ctx.Instance;

				dv = ri.Ctx.Values;
			}
			else if (Ctx.Instance is FormsActionRulesInstance)
			{
				var ri = (FormsActionRulesInstance)Ctx.Instance;

				dv = ri.Ctx.Values;
			}

			if (dv == null)
				throw new NotSupportedException();

			var variables = Ctx.Variables;

			foreach (var v in dv)
				variables[v.Key] = v.Value;
		}
	}
}